package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.service.RoomService;
import cn.bytecollege.service.UserMessageService;
import cn.bytecollege.service.VipService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author LH
 */
@WebServlet(name="MainController",value = "/main")
public class MainController extends HttpServlet {
    private UserMessageService userMessageService=null;
    private VipService vipService=null;
    private RoomService roomService=null;

    public MainController(){
        userMessageService=new UserMessageService();
        vipService=new VipService();
        roomService=new RoomService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收请求后直接调用service方法
        //1、查询VIP用户数
        int vipNumber=vipService.count();
        //2、查询已入住的用户数
        int userNumber=userMessageService.countByState();
        //3、查询剩余空房数
        int emptyRoomNumber=roomService.count()- roomService.count(1);

        //封装数据
        request.setAttribute("vipNumber",vipNumber);
        request.setAttribute("userNumber",userNumber);
        request.setAttribute("emptyRoomNumber",emptyRoomNumber);

        //  跳转页面
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"main.jsp").forward(request,response);

    }
}
